﻿Imports Abyx.SVCMS.Library
Imports Abyx.SVCMS.Library.DB
Imports Abyx.WindowsExtendedControls

Public Class STK_CatalogItemAssociationToSpares_Form
    Implements ICarfDataEditingChildWindow

    Dim catalog As New CatalogItemTypeCollection
    Dim catSpares As New EquipmentSparepartsTypeCollection
    Dim spares As New StockItemTypeCollection

#Region "Events"
    Private Sub STK_CatalogItemAssociationToSpares_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Me.InitializeThisFormAndControlSettings()
        Refresh_CatalogData()
        Refresh_CatSparesData()
        Refresh_SparesData()
        Refresh_SparesList()
        Refresh_CatalogItemsTree()

    End Sub
    Private Sub STK_CatalogItemAssociationToSpares_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.InitializeThisFormAndControlSettings(True)
    End Sub
    Private Sub Close_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Close_ImageButton.Click
        Me.Close()
    End Sub
    Private Sub esFilter_TextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles esFilter_TextBox.TextChanged
        Refresh_CatalogItemsTree()
    End Sub
    Private Sub spFilter_TextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles spFilter_TextBox.TextChanged
        Refresh_SparesList()
    End Sub
    Private Sub Spares_DetailListBox_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Spares_DetailListBox.MouseDown
        If (Spares_DetailListBox.SelectedItem IsNot Nothing) _
            AndAlso (TypeOf Spares_DetailListBox.SelectedItem Is DetailListBoxItem) Then
            Dim item As DetailListBoxItem = Spares_DetailListBox.SelectedItem
            Spares_DetailListBox.DoDragDrop(item.DataObject, DragDropEffects.Link + DragDropEffects.Scroll)
        End If
    End Sub
    Private Sub CatSpares_CTreeView_DragOver(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles CatSpares_CTreeView.DragOver

        Dim p As Point = CatSpares_CTreeView.PointToClient(New Point(e.X, e.Y))
        If p <> Point.Empty Then
            Dim cNode As cTreeViewNode = CatSpares_CTreeView.GetNodeAt(p)
            If cNode IsNot Nothing Then
                Dim parentNode As cTreeViewNode = Nothing
                If cNode.NodeType = "CatalogItem" Then
                    parentNode = cNode
                Else
                    If cNode.Parent IsNot Nothing Then
                        parentNode = cNode.Parent
                    End If
                End If
                If (parentNode IsNot Nothing) _
                    AndAlso (parentNode.NodeType = "CatalogItem") Then
                    If (e.Data IsNot Nothing) _
                                      AndAlso (e.Data.GetDataPresent(GetType(StockItemType))) Then
                        e.Effect = DragDropEffects.Link
                    Else
                        e.Effect = DragDropEffects.None
                    End If
                Else
                    e.Effect = DragDropEffects.None
                End If
            Else
                e.Effect = DragDropEffects.None
            End If
        End If

        'e.Effect = DragDropEffects.Link
    End Sub
    Private Sub CatSpares_CTreeView_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles CatSpares_CTreeView.DragDrop

        'collect necessary data
        Dim p As Point = CatSpares_CTreeView.PointToClient(New Point(e.X, e.Y))
        Dim cNode As cTreeViewNode = CatSpares_CTreeView.GetNodeAt(p)
        Dim parentNode As cTreeViewNode = Nothing
        If cNode.NodeType = "CatalogItem" Then
            parentNode = cNode
        Else
            If cNode.Parent IsNot Nothing Then
                parentNode = cNode.Parent
            End If
        End If
        Dim spare As StockItemType = e.Data.GetData(GetType(StockItemType))
        Dim cat As CatalogItemType = parentNode.DataObject
        Dim es As New EquipmentSparepartsType
        With es
            .EquipmentSparesId = Guid.NewGuid
            .SparesStockId = spare.StockItemId
            .EquipmentId = cat.EquipmentId
        End With

        'update the database
        Dim stkMan As New StkManager(AppSettings.LoggedUser)
        Try
            stkMan.Update_EquipmentSpareparts(es)
            'insert the node
            Dim node As New cTreeViewNode(es.ToString, "SpareItem", ActionButtonTypesEnum.Delete, es)
            parentNode.Nodes.Add(node)
            parentNode.ExpandAll()
        Catch ex As SqlClient.SqlException
            If ex.Message.Contains("constraint") Then
                AppSettings.MainForm.StatusInfoMsgPut("Association already exists, please select another one")
            Else
                AMsg.ShowDialog("Error: " & ex.Message, MsgBoxStyle.Critical, ex, es)
            End If
        Catch ex As Exception
            AMsg.ShowDialog("Error: " & ex.Message, MsgBoxStyle.Critical, ex, es)
        End Try

    End Sub
    Private Sub CatSpares_CTreeView_ActionButtonClicked(Node As WindowsExtendedControls.cTreeViewNode, actionButton As WindowsExtendedControls.ActionButtonTypesEnum) Handles CatSpares_CTreeView.ActionButtonClicked

        If actionButton = ActionButtonTypesEnum.Delete Then
            'Update the database
            Dim stkman As New StkManager(AppSettings.LoggedUser)
            Dim es As EquipmentSparepartsType = Node.DataObject
            Try
                stkman.Delete_EquipmentSpareparts(es)
                'remove the node
                Node.Remove()
            Catch ex As Exception
                AMsg.ShowDialog("Error: " & ex.Message, MsgBoxStyle.Critical, ex, es)
            End Try
        End If

    End Sub

#End Region

#Region "ICARF Implementations"
    Public Function GetFormInstanceForDataObject(DataObject As Object) As System.Windows.Forms.Form Implements ICarfDataEditingChildWindow.GetFormInstanceForDataObject
        Return Nothing
    End Function
    Public Sub OrderRefreshRelatedDataControls(Optional DataItem As Object = Nothing, Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.OrderRefreshRelatedDataControls

        Dim main As ICarfParentWindow = Me.MdiParent
        main.OrderRefreshRelatedDataControls(Me, _
                                             RelatedControlDataCategoriesEnum.StockItem, _
                                             DataItem, _
                                             ActionOnDataItem)

    End Sub
    Public Sub ProcessRefreshRelatedDataControls(sender As System.Windows.Forms.Form, DataTypeCategory As RelatedControlDataCategoriesEnum, Optional DataItem As Object = Nothing, Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.ProcessRefreshRelatedDataControls

        Select Case DataTypeCategory
            Case Is = RelatedControlDataCategoriesEnum.StockItem, RelatedControlDataCategoriesEnum.EquipmentCatalog

                AppSettings.MainForm.StatusInfoMsgPut("Data displayed were updated else where, and hence was refreshed automatically")
                Refresh_CatalogData()
                Refresh_CatSparesData()
                Refresh_SparesData()
                Refresh_SparesList()
                Refresh_CatalogItemsTree()

        End Select

    End Sub
    Public Sub Refresh_ControlsWithDataObject() Implements ICarfDataEditingChildWindow.Refresh_ControlsWithDataObject

    End Sub
    Public Sub Refresh_DataObjectWithControls() Implements ICarfDataEditingChildWindow.Refresh_DataObjectWithControls

    End Sub
    Public Sub InitializeThisFormAndControlSettings(Optional OrderUpdateToFile As Boolean = False) Implements ICarfDataEditingChildWindow.InitializeThisFormAndControlSettings
        'init general settings
        AppSettings.InitFormSettings(Me, OrderUpdateToFile)
    End Sub
#End Region

#Region "Private Procedures"
    Private Sub Refresh_CatalogData()
        Dim catMan As New SqcManager(AppSettings.LoggedUser)
        Me.catalog = catMan.Get_CatalogItems_All()
    End Sub
    Private Sub Refresh_CatSparesData()
        Dim stkMan As New StkManager(AppSettings.LoggedUser)
        Me.catSpares = stkMan.Get_EquipmentSpareparts_All_Col
    End Sub
    Private Sub Refresh_SparesData()
        Dim stkMan As New StkManager(AppSettings.LoggedUser)
        Me.spares = stkMan.Get_StockItem_All_Col
    End Sub
    Private Sub Refresh_SparesList()

        Spares_DetailListBox.Items.Clear()
        If Me.spares.Count > 0 Then
            If spFilter_TextBox.Text = "" Then
                For i As Integer = 0 To Me.spares.Count - 1
                    Dim sp As StockItemType = Me.spares(i)
                    Spares_DetailListBox.Items.Add(sp.ToDetailsListBoxItem)
                Next
            Else
                For i As Integer = 0 To Me.spares.Count - 1
                    Dim sp As StockItemType = Me.spares(i)
                    If sp.ToString.ToLower.Contains(spFilter_TextBox.Text.ToLower) Then
                        Spares_DetailListBox.Items.Add(sp.ToDetailsListBoxItem)
                    End If
                Next
            End If
        End If

    End Sub
    Private Sub Refresh_CatalogItemsTree()

        CatSpares_CTreeView.Nodes.Clear()
        If catalog.Count > 0 Then
            If esFilter_TextBox.Text = "" Then
                For i As Integer = 0 To catalog.Count - 1
                    Dim eq As CatalogItemType = catalog(i)
                    Dim node As New cTreeViewNode(eq.ToString, "CatalogItem", ActionButtonTypesEnum.None, eq)
                    CatSpares_CTreeView.Nodes.Add(node)
                    Refresh_CatalogItemSpares(eq, node)
                Next
            Else
                For i As Integer = 0 To catalog.Count - 1
                    Dim eq As CatalogItemType = catalog(i)
                    If eq.ToString.ToLower.Contains(esFilter_TextBox.Text.ToLower) Then
                        Dim node As New cTreeViewNode(eq.ToString, "CatalogItem", ActionButtonTypesEnum.None, eq)
                        CatSpares_CTreeView.Nodes.Add(node)
                        Refresh_CatalogItemSpares(eq, node)
                    End If
                Next
            End If
        End If
        CatSpares_CTreeView.ExpandAll()

    End Sub
    Private Sub Refresh_CatalogItemSpares(eq As CatalogItemType, itemNode As cTreeViewNode)

        itemNode.Nodes.Clear()
        Dim fcatSpares As IEnumerable(Of EquipmentSparepartsType) = From fsp As EquipmentSparepartsType In Me.catSpares
                                                                    Where fsp.EquipmentId.Equals(eq.EquipmentId)
        If (fcatSpares IsNot Nothing) _
            AndAlso (fcatSpares.Count > 0) Then
            For i As Integer = 0 To fcatSpares.Count - 1
                Dim catSpare As EquipmentSparepartsType = fcatSpares(i)
                Dim node As New cTreeViewNode(catSpare.ToString, "SpareItem", ActionButtonTypesEnum.Delete, catSpare)
                itemNode.Nodes.Add(node)
            Next
        End If

    End Sub
#End Region






End Class